﻿using System;
using System.Net;
using System.Net.Sockets;

namespace Transformable_Engine_v2.Engine.Network
{
    public static class Helper
    {
        public static IPAddress FindLocalIpAddress()
        {
            var addresses = Dns.GetHostAddresses(Dns.GetHostName());

            foreach (var ipAddress in addresses)
            {
                if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
                    return ipAddress;
            }

            throw new Exception("The IP address could not be found");
        }

        public static IPAddress IpAddressFromHostName(string hostName, AddressFamily addressType = AddressFamily.InterNetwork)
        {
            var addresses = Dns.GetHostAddresses(hostName);

            foreach (var ipAddress in addresses) {
                if(ipAddress.AddressFamily == addressType)
                    return ipAddress;
            }

            throw new Exception("Host has no address of this type");
        }
    }
}